package onjava.generics;


// generics/ArrayMaker.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.lang.reflect.*;
import java.util.*;

/**
 * 
 * 类型擦除的奥秘 -> 边界的行为
 * 
 * 创建数组
 *
 * @author Lucky Yang
 */
public class ArrayMaker<T> {
  private Class<T> kind;

  public ArrayMaker(Class<T> kind) {
    this.kind = kind;
  }

  @SuppressWarnings("unchecked")
  T[] create(int size) {
    // 泛型中创建数组的最佳方式
    return (T[]) Array.newInstance(kind, size);
  }

  public static void main(String[] args) {
    ArrayMaker<String> stringMaker = new ArrayMaker<>(String.class);
    String[] stringArray = stringMaker.create(9);
    System.out.println(Arrays.toString(stringArray));
  }
}
/*
 * Output:
 * [null, null, null, null, null, null, null, null, null]
 */
